package com.sangmo.fts.api.inventory;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.process.inventory.interfaces.IfStockConfirmRequest;
import com.sangmo.fts.process.inventory.interfaces.IfStockCreation;
import com.sangmo.fts.process.inventory.model.IfStock;
import com.sangmo.fts.process.inventory.model.IfStockItem;
import com.sangmo.fts.process.inventory.model.IfWaybill;
import com.sangmo.fts.process.inventory.service.IfStockService;
import com.sangmo.fts.restructure.meta.TrainingApi;
import com.sangmo.fts.restructure.meta.TrainingProcess;
import com.sangmo.fts.training.model.TrainingHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@TrainingApi
@Controller
@RequestMapping("/ifStock/v1")
@Access("ifStock")
public class IfStockApi {

    @Autowired
    private IfStockService ifStockService;

    @Access(":create")
    @PostMapping(value = "/{trainingId}/create")
    public @ResponseBody
    ApiResponse<IfStock> create(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid IfStockCreation obj) {
        obj.setTrainingId(trainingId);
        return ApiResponse.make(ifStockService.create(obj));
    }

    @Access(":read")
    @PostMapping("/{trainingId}/search")
    public @ResponseBody ApiResponse<PageResult<IfStock>> search(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("IfStock.search", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ifStockService.search(ps));
    }

    @TrainingProcess(node = TrainingHistory.IfProcessNodes.IF_STOCK_STORAGE_APPROVE_6)
    @Access(":approve")
    @PostMapping("/{trainingId}/{id}/approve")
    public @ResponseBody ApiResponse approve(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ifStockService.approve(id);
        return ApiResponse.make();
    }

    @Access(":read")
    @PostMapping("/{trainingId}/{id}/items/list")
    public @ResponseBody ApiResponse<PageResult<IfStockItem>> listGoodsItems(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long loanId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        return ApiResponse.make(ifStockService.listItemsByPage(loanId, ps));
    }


    @Access(":selection")
    @PostMapping({"/{trainingId}/selection"})
    @ResponseBody
    public ApiResponse<PageResult<IfStock>> selection(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("IfStock.selection", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ifStockService.selection(ps));
    }

}
